package com.hy.common.excel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.common.consts.CommonConst;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.MyObjectUtils;
import com.hy.entity.dto.DepartmentSearchDTO;

import com.hy.entity.pojo.HyDepartment;

import com.hy.entity.vo.HyDepartmentVO;

import com.hy.mapper.HyDepartmentMapper;

import com.hy.service.HyDepartmentService;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
public class HyDepartmentExport extends AbstractExcelExport<HyDepartmentVO>{

    private Integer count;

    @Resource
    private HyDepartmentMapper departmentMapper;
    @Resource
    private HyDepartmentService departmentService;
    public void exportWithBigData(HttpServletResponse response, String fileName,  DepartmentSearchDTO searchDTO){
        List<String> roles = CommonUtils.getCurrentUser().getRoles();
        this.count=departmentMapper.selectCount(new LambdaQueryWrapper<HyDepartment>().and(wapper->
                {        wapper
                        .eq(MyObjectUtils.isNotBlank(searchDTO.getDepartmentName()),HyDepartment::getDepartmentName,searchDTO.getDepartmentName())
                        .eq(!ObjectUtils.isEmpty(searchDTO.getManagerId()),HyDepartment::getManagerId,searchDTO.getManagerId())
                        .eq(HyDepartment::getDel,searchDTO.getDel());
                    if(CommonUtils.isMember(roles, CommonConst.SYSTEM_ROLE_NAME)){
                        wapper.eq(!ObjectUtils.isEmpty(searchDTO.getUnitId()),HyDepartment::getUnitId,searchDTO.getUnitId());

                    }else {
                        wapper.eq(!ObjectUtils.isEmpty(searchDTO.getUnitId()),HyDepartment::getUnitId,CommonUtils.getCurrentUser().getUnitId());
                    }
                }
        )).intValue();
        this.exportWithBigData(response, fileName, null, BeanUtil.beanToMap(searchDTO), HyDepartmentVO.class);
    }


    //计算出导出的数量
    @Override
    protected Integer dataTotalCount(Map<String, Object> queryCondition) {
        return this.count;
    }

    /**
     * 每一个sheet存放的数据总数
     * @return
     */
    @Override
    protected Integer eachSheetTotalCount() {//每一个sheet存放的数据总数
        return 500000;
    }

    @Override
    protected Integer eachTimesWriteSheetTotalCount() {//每次写入sheet的总数
        return 5000;
    }

    @Override
    protected void buildDataList(List<HyDepartmentVO> resultList, Map<String, Object> queryCondition, Integer pageNo, Integer pageSize) {

        for (HyDepartmentVO hyDepartmentVO : departmentService.listPage(queryCondition, pageNo, pageSize)) {
            resultList.add(hyDepartmentVO);
        }


//        if(CollUtil.isNotEmpty(entityDoList)){
//            entityDoList.forEach(item -> {
//                resultList.add(Arrays.asList(item.getDepartmentId().toString(),item.getDepartmentName(),item.getDepartmentDetail(),
//                        item.getManagerName(),item.getUnit(),item.getCreateDate()
//                ));
//            });
//        }

    }
}
